<?php

namespace App\Services;

use App\Models\GoodsOrder;
use App\Models\GoodsOrderDetails;
use App\Models\Supplier;
use App\Models\SupplierOrder;
use App\Models\Users;
use TencentCloud\Cpdp\V20190820\Models\Order;

class GoodsOrderService  extends BaseService
{
    /**
     * 获取订单列表
     * @return
     */
    public function list($page,$limit,$sort,$order,$order_num) {
        $query = GoodsOrder::leftJoin('goods_order_details', 'goods_orders.order_num', '=', 'goods_order_details.order_num')
            ->select('goods_orders.*',
                'num','price','title','image'
            );
        if(isset($order_num)){
            $query->where('goods_orders.order_num',$order_num);
        }
        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 获取未分配的供应商订单列表
     * @return
     */
    public function unallocatedSupplierOrderlist($page,$limit,$sort,$order,$order_num,$username,$order_type) {
        $supplierOrderRes = SupplierOrder::get();
        $supplierOrderArrIds = [];
        if($supplierOrderRes){
            $supplierOrderArr = $supplierOrderRes->toArray();
            $supplierOrderArrIds = array_column($supplierOrderArr,'order_num','id');
        }
        $query = GoodsOrder::whereNotIn('order_num', $supplierOrderArrIds)
                    ->leftJoin('schools', 'goods_orders.school_id', '=', 'schools.id')
                    ->leftJoin('users', 'goods_orders.users_id', '=', 'users.id')
                    ->select('goods_orders.*',
                     'schools.name as schoolName','users.username'
                    );
        if(isset($order_num)){
            $query->where('goods_orders.order_num',$order_num);
        }
        if(isset($order_type)){
            $query->where('goods_orders.order_type',$order_type);
        }
        if(isset($username)){
            $query->where('users.username',$username);
        }

        $res =  $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);

        return $res;
    }
    /**
     * 获取已经分配普通供应商订单列表
     * @return
     */
    public function supplierOrderlist($page,$limit,$sort,$order,$supplier_id,$order_num,$username,$order_type) {
        $query = SupplierOrder::leftJoin('goods_orders', 'supplier_orders.order_num', '=', 'goods_orders.order_num')
            ->leftJoin('schools', 'goods_orders.school_id', '=', 'schools.id')
            ->leftJoin('users', 'goods_orders.users_id', '=', 'users.id')
            ->leftJoin('suppliers', 'supplier_orders.supplier_id', '=', 'suppliers.id')
            ->select('goods_orders.*',
                'schools.name as schoolName','users.username','suppliers.name as supplierName'
            );
        if(isset($supplier_id)){
            $query->where('supplier_id',$supplier_id);
        }
        if(isset($order_num)){
            $query->where('goods_orders.order_num',$order_num);
        }
        if(isset($order_type)){
            $query->where('goods_orders.order_type',$order_type);
        }
        if(isset($username)){
            $query->where('users.username',$username);
        }
        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 未分配给供应商游客订单列表
     * @return
     */
    public function touristSupplierOrderlist($page,$limit,$sort,$order,$order_num) {
        $supplierOrder = SupplierOrder::get();
        $supplierOrderArr = [];
        if(count($supplierOrder) > 0){
            $supplierOrderArr = $supplierOrder->toArray();
            $supplierOrderArr = array_column($supplierOrderArr,'order_num','id');
        }
        $query = GoodsOrder::whereNotIn('goods_orders.order_num',$supplierOrderArr);
        if(isset($order_num) && empty($order_num)){
            $query->where('goods_orders.order_num',$order_num);
        }
        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);

    }
    /**
     * 获取订单信息
     * @return
     */
    public function info($order_num) {
        $orderDetails = GoodsOrderDetails::where('order_num',$order_num)->get();
        return $orderDetails;
    }
    /**
     * 获取用户订单信息
     * @return
     */
    public function userInfo($users_id,$order_num,$title) {
        $query = GoodsOrder::where('users_id',$users_id);
        if(isset($order_num)){
            $query->where('order_num',$order_num);
        }
        $order = $query->select('order_num','total_price','address','logistics','logistics_order','logistics_order',
            'status','pay_time','consignment_time','end_time'
        )->get();
        $orderNumArr = $order->pluck('order_num');
        if(count($orderNumArr) > 0){
            $query=GoodsOrderDetails::whereIn('order_num',$orderNumArr);
            if(isset($title)){
                $query->where('title', 'like', '%'.$title.'%');
            }
            $orderDetails = $query->get();
            foreach ($order as $k => $v) {
                $order[$k]['details'] = $orderDetails;
                foreach ($orderDetails as $v2) {
                    if($v['order_num'] == $v2['order_num']){
                        $order[$k]['details'] = $orderDetails;
                    }
                }
            }
            if(count($orderDetails) > 0){
                return $order;
            }
        }
        return [];
    }
}
